package com.ics.cmsadmin.modules.basic.service.impl;

import com.ics.cmsadmin.frame.core.enums.LevelEnum;
import com.ics.cmsadmin.modules.basic.bean.*;
import com.ics.cmsadmin.modules.basic.dao.CollectionDao;
import com.ics.cmsadmin.modules.basic.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionDao collectionDao;

    @Autowired
    private CourseService courseService;

    @Autowired
    private UnitService unitService;

    @Autowired
    private TopicService topicService;

    @Autowired
    private PointService pointService;

    @Override
    public Map<LevelEnum, List> getCollectionList(String userId) {
        Map<LevelEnum, List> ret = new HashMap<>();
        for (LevelEnum i = LevelEnum.course; i != LevelEnum.subject; i = i.next()) {
            ret.put(i, new LinkedList());
        }
        List<Collect> collects = collectionDao.findByUserId(userId);
        for (Collect collect : collects) {
            List list = ret.get(collect.getLevelName());
            switch (collect.getLevelName()) {
                case course:
                    CourseBean course = courseService.queryById(collect.getLevelId());
                    if (course != null) {
                        list.add(course);
                    }
                    break;
                case unit:
                    UnitBean unit = unitService.queryById(collect.getLevelId());
                    if (unit != null) {
                        list.add(unit);
                    }
                    break;
                case topic:
                    TopicBean topic = topicService.queryById(collect.getLevelId());
                    if (topic != null) {
                        list.add(topic);
                    }
                    break;
                case point:
                    PointBean point = pointService.queryById(collect.getLevelId());
                    if (point != null) {
                        list.add(point);
                    }
                    break;
                default:
                    break;
            }
        }
        return ret;
    }

    @Override
    public Integer queryTotal(LevelEnum levelName, String levelId) {
        return collectionDao.queryTotal(levelName, levelId);
    }

    @Override
    public int delete(Collect collect) {
        return collectionDao.delete(collect);
    }

    @Override
    public int insert(Collect collect) {
        return collectionDao.insert(collect);
    }

    @Override
    public Boolean select(Collect collect) {
        return collectionDao.select(collect);
    }

    @Override
    public int toggleCollect(Collect collect) {
        Boolean collected = collectionDao.select(collect);
        if (collected) {
            return collectionDao.delete(collect);
        } else {
            return collectionDao.insert(collect);
        }
    }
}
